package com.example.secondhandshop.ui.fragment;

import static androidx.core.content.ContextCompat.registerReceiver;

import androidx.fragment.app.Fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.secondhandshop.R;
import com.example.secondhandshop.bean.Allchatuser;
import com.example.secondhandshop.bean.Chatitem;
import com.example.secondhandshop.model.ChatitemModel;
import com.example.secondhandshop.model.ChatuserModel;
import com.example.secondhandshop.ui.activity.LoginAcitivity;
import com.example.secondhandshop.ui.activity.Message_center;
import com.example.secondhandshop.ui.adapter.AllchatitemAdapter;
import com.example.secondhandshop.ui.adapter.AllchatuserAdapter;
import com.example.secondhandshop.ui.adapter.MessageconterAdapter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import android.widget.ListView;
import android.widget.Toast;

public class MessageFragment extends Fragment implements ChatuserModel.OnProductsFetchedListener {
    private RecyclerView recyclerView;
    private AllchatuserAdapter allchatuserAdapter;
    private ChatuserModel chatuserModel;
    private List<Allchatuser> allchatusers;
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_message, container, false);
        chatuserModel = new ChatuserModel();  // 确保这不是一个内部类
        chatuserModel.getallchatuser(LoginAcitivity.userid, this); // 使用 'this' 作为回调
        allchatuserAdapter = new AllchatuserAdapter(new ArrayList<>());
        recyclerView= view.findViewById(R.id.lv_messageuser_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(allchatuserAdapter);
        return view;
    }

    @Override
    public void onFailure(String message) {
        Toast.makeText(getActivity(), "Failed to fetch chat users: " + message, Toast.LENGTH_LONG).show();
    }

    @Override
    public void onSuccess(List<Allchatuser> allchatusers) {
        // 使用 runOnUiThread 确保在主线程中更新 UI
        System.out.println(" allchatusers is "+allchatusers);
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (allchatuserAdapter == null) {
                    allchatuserAdapter = new AllchatuserAdapter(allchatusers);
                    recyclerView.setAdapter(allchatuserAdapter);
                } else {
                    allchatuserAdapter.updateChatUsers(allchatusers);
                    allchatuserAdapter.notifyDataSetChanged();
                }
            }
        });
    }
    private BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            int value = intent.getIntExtra("key",0);
            // 处理数据
            int fromserid=intent.getIntExtra("fromuserid",0);
            for(int i=0;i<allchatusers.size();i++){
                if(allchatusers.get(i).getFromUserId()==fromserid){
                    allchatusers.get(i).setUnReadNum(0);
                    allchatuserAdapter.notifyDataSetChanged();
                }
            }

        }
    };

    @Override
    public void onResume() {
        super.onResume();
        IntentFilter filter = new IntentFilter("MessageFragemt.class");
        registerReceiver(receiver, filter);
    }

    private void registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
    }

    @Override
    public void onPause() {
        super.onPause();
        unregisterReceiver(receiver);
    }

    private void unregisterReceiver(BroadcastReceiver receiver) {

    }

}